<template>
  <a-modal v-model="modalVisible" :title="modalTitle" width="1200px" @ok="submit()" :maskClosable="true">
    <a-form-model :model="beanInfo" layout="inline" @submit="search" class="custom">
      <a-row :gutter="20">
        <a-col span="8" v-if="currentType === merchantType.NATURAL_MERCHANT || currentType === merchantType.SELECTED_MERCHANT || currentType === merchantType.LOW_TXN_MERCHANT || currentType === merchantType.NEW_MERCHANT">
          <a-form-model-item label="行业类别编码" label-align="right">
            <a-input v-model="beanInfo.industryType" class="input"  placeholder="请输入行业类别编码"/>
          </a-form-model-item>
        </a-col>
        <a-col span="8" v-if="currentType === merchantType.NATURAL_MERCHANT || currentType === merchantType.SELECTED_MERCHANT || currentType === merchantType.LOW_TXN_MERCHANT || currentType === merchantType.NEW_MERCHANT">
          <a-form-model-item label="商户属性" label-align="right">
            <a-select v-model="beanInfo.mchntAttr" class="input" placeholder="请选择商户属性">
              <a-select-option value="01">实体特约商户</a-select-option>
              <a-select-option value="10">网络特约商户</a-select-option>
              <a-select-option value="11">实体兼网络特约商户</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col span="8" v-if="currentType === merchantType.RISK_MERCHANT">
          <a-form-model-item label="风险等级" label-align="right">
            <a-select v-model="beanInfo.riskLevel" class="input" placeholder="请选择风险等级">
              <a-select-option value="01">一级</a-select-option>
              <a-select-option value="02">二级</a-select-option>
              <a-select-option value="03">三级</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col span="8">
          <a-form-model-item label="收单机构" label-align="right">
            <a-input v-model="beanInfo.signInstId" placeholder="请输入收单机构"/>
          </a-form-model-item>
        </a-col>
        <a-col span="8">
          <a-form-model-item label="商户类型" label-align="right">
            <a-select v-model="beanInfo.mchntType" class="input" placeholder="请选择商户类型">
              <a-select-option value="01">普通商户</a-select-option>
              <a-select-option value="10">连锁商户</a-select-option>
              <a-select-option value="11">连锁商户门店</a-select-option>
              <a-select-option value="20">小薇商户</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col span="8">
          <a-form-model-item label="商户等级" label-align="right">
            <a-input v-model="beanInfo.mchntLevel" class="input" placeholder="请输入商户等级"/>
          </a-form-model-item>
        </a-col>
        <a-col span="8" v-if="currentType === merchantType.NATURAL_MERCHANT">
          <a-form-model-item label="商户号" label-align="right">
            <a-input v-model="beanInfo.mchntNo" class="input" placeholder="请输入商户号"/>
          </a-form-model-item>
        </a-col>

        <a-col span="8" v-if="currentType === merchantType.SELECTED_MERCHANT">
          <a-form-model-item label="抽检方式" label-align="right">
            <a-select v-model="beanInfo.spotCheckMethod" class="input" placeholder="请选择抽检方式">
              <a-select-option value="01">按机构</a-select-option>
              <a-select-option value="02">随机</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col span="8" v-if="currentType === merchantType.SELECTED_MERCHANT">
          <a-form-model-item label="抽检数量" label-align="right">
            <a-input v-model="beanInfo.spotCheckMchntNum" class="input" placeholder="请输入抽检数量"/>
          </a-form-model-item>
        </a-col>

        <a-col span="8" v-if="currentType === merchantType.LOW_TXN_MERCHANT">
          <a-form-model-item label="月份" label-align="right">
            <a-select v-model="beanInfo.lowVolumeMonth" class="input" placeholder="请选择月份">
              <a-select-option value="01">一月</a-select-option>
              <a-select-option value="02">二月</a-select-option>
              <a-select-option value="03">三月</a-select-option>
              <a-select-option value="04">四月</a-select-option>
              <a-select-option value="05">五月</a-select-option>
              <a-select-option value="06">六月</a-select-option>
              <a-select-option value="07">七月</a-select-option>
              <a-select-option value="08">八月</a-select-option>
              <a-select-option value="09">九月</a-select-option>
              <a-select-option value="10">十月</a-select-option>
              <a-select-option value="11">十一月</a-select-option>
              <a-select-option value="12">十二月</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col span="8" v-if="currentType === merchantType.LOW_TXN_MERCHANT">
          <a-form-model-item label="交易笔数小于" label-align="right">
            <a-input v-model="beanInfo.lowVolumeLessThan" class="input" placeholder="请输入交易笔数小于"/>
          </a-form-model-item>
        </a-col>
        <a-col span="8" v-if="currentType === merchantType.NEW_MERCHANT">
          <a-form-model-item label="入驻时间" label-align="right">
            <a-range-picker v-model="beanInfo.dateRange" class="input"/>
          </a-form-model-item>
        </a-col>

        <a-col span="24" v-if="currentType === merchantType.NATURAL_MERCHANT">
          <a-form-model-item label="上次巡检方式" label-align="right">
            <a-checkbox-group v-model="beanInfo.inspectType" class="input" :options="[
                {value: '10', label: '商户自助巡检'},
                {value: '20', label: '线下现场巡检'},
                {value: '00', label: '电话巡检'},
              ]"></a-checkbox-group>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row type="flex" justify="center">
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-row>
    </a-form-model>
    <div class="section">
      <div class="btn-group">
        <a-button type="primary" @click="addInspectTask()">生成巡检任务</a-button>
        <a-button type="primary" @click="addAll()">全部生成巡检任务</a-button>
      </div>
      <a-table ref="table" :columns="columns" :dataSource="dataSource" rowKey="id" :loading="loading"
               :pagination="pagination" @change="search" @showSizeChange="search">

      </a-table>
    </div>
  </a-modal>
</template>

<script>
import EntityView from '../../../../../components/common/entityView/EntityView'
import FormItem from '../../../../../components/common/entityView/FormItem'

export default {
  name: 'ITMOperation',
  inject: ['merchantType'],
  components: {
    EntityView,
    FormItem
  },
  props: {
    modalName: String,
    currentType: String
  },
  data: function () {
    return {
      query:{},
      modalVisible: false,
      modalTitle: '',
      operatorType: 'add',
      beanInfo: {
        industryType: '',
        mchntAttr: '',
        signInstId: '',
        mchntType: '',
        mchntLevel: '',
        mchntNo: '',
        inspectType: [],
      },
      rules: {
        industryType: [{required: true, message: '请输入行业类别编码编号', trigger: 'blur'}],
        destCatalogCd: [{required: true, message: '请输入经营类目编码', trigger: 'blur'}],
        destChannel: [{required: true, message: '请选择通道标识', trigger: 'blur'}],
        publicPrivateFlag: [{required: true, message: '请选择对公对私标识', trigger: 'blur'}],
      },
      selectOptions: [],
      loading: false,
      columns: [{
        title: '商户号',
        dataIndex: 'mchntNo',
      }, {
        title: '商户名称',
        dataIndex: 'mchntName',
      }, {
        title: '商户类型',
        dataIndex: 'mchntType',
        customRender (text) {
          return {'00': '普通商户', '10': '连锁商户', '11': '连锁商户门店', '20': '小微商户',}[text]
        }
      }, {
        title: '收单机构',
        dataIndex: 'signInstName',
      }, {
        title: '拓展经理',
        dataIndex: 'managerName',
      }, {
        title: '商户等级',
        dataIndex: 'mchntLevelName',
      }, {
        title: '上次巡检时间',
        dataIndex: 'createTime',
      }, {
        title: '上次巡检方式',
        dataIndex: 'prevInspectType',
        customRender (text) {
          return {'00': '电话巡检', '10': '商户自助巡检', '20': '线下现场巡检'}[text]
        }
      }, {
        title: '备注',
        dataIndex: 'remark',
      },],
      dataSource: [],
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) =>
          `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      },
    }
  },
  methods: {

    addInspectTask(){
      var saveFlag = "inspectTaskSaveFlag";
      if(!isExport(saveFlag)){
        this.$message.warning("生成巡检任务数据处理中，请稍后再试！");
      }
      var mchntNos = [];
      var datas = $(this).getSelectedRowdatas();
      // var datas = $grid.getSelectedRowdatas();
      var params = {};
       params.instId = this.query.instId;
       params.mchntNo = this.query.mchntNo;
       params.inspectTaskType = this.currentType;
        this.$get('/admin/merParam/inspectTaskInfo/edit',{...params
        }).then((r)=> {
          this.$emit('')
        });

    },

    addAll(){
      var saveFlag = "inspectTaskSaveFlag";
      if(!isExport(saveFlag)){
        Global.notify("error", "生成巡检任务数据处理中，请稍后再试！");
        return false;
      }
      e.preventDefault();
      var inspectTaskType = $('#mchnt input#inspectTaskType').val();
      var industryType = $('#mchnt [name=industryType]').val();
      var mchntAttr = "";//$('#mchnt [name=mchntAttr]').val();
      var signInstId = $('#mchnt [name=signInstId]').val();
      var mchntType = $('#mchnt [name=mchntType]').val();
      var mchntLevel = "";//$('#mchnt [name=mchntLevel]').val();
      var mchntNo = $('#mchnt [name=mchntNo]').val();
      checkboxSetValue();
      var inspectTp = $('#mchnt [name=inspectTp]').val();
      var url = WEB_ROOT + '/admin/merParam/inspectTaskInfo/editAll?inspectTaskType=' + inspectTaskType+'&instId=${requestScope.instId}&flag=getAll'
        +'&industryType='+industryType +'&mchntAttr='+mchntAttr +'&signInstId='+signInstId +'&mchntType='+mchntType +'&mchntLevel='+mchntLevel +'&inspectTp='+inspectTp+'&mchntNo='+mchntNo;
      $grid.popupDialog({url: url, title: "全部生成巡检任务"});
    },

    search(e) {
      let params = {}
      params.instId = this.query.instId
      params.mchntName = this.query.mchntName
      params.mchntNo = this.query.mchntNo
      params.signInstId = this.query.signInstId
      params.mchntType = this.query.mchntType
      params.inspectType = this.query.inspectType
      params.inspectInstId = this.query.inspectInstId
      this.fetch({
        ...params,
      })
    },

    fetch(params = {}) {
      // params.userId = this.user.id;
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.table.pagination.current = this.paginationInfo.current
        this.$refs.table.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        params.pageNum = this.paginationInfo.current
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }
      this.$get('/admin/merParam/inspectTaskDetailQuery/list', {
        ...params
      }).then((r) => {
        let data = r.data
        const pagination = {...this.pagination}
        pagination.total = data.total
        this.dataSource = data.rows
        this.pagination = pagination
        this.loading = false
      })
    },
    openModal (type = 'view', beanInfo) {
      if(type === 'add'){this.search()}
      this.modalTitle = {add: `新增${this.modalName}`, edit: `编辑${this.modalName}`, view: `${this.modalName}详情`}[type]
      this.modalVisible = true
      this.$nextTick(() => {
        this.operatorType = type
        if (type === 'edit' || type === 'view' ) {
          this.setBeanInfo(beanInfo)
        }
      })
    },
    async submit () {
      let res
      switch (this.operatorType) {
        case 'add':
          res = await this.add()
          break
        case 'edit':
          res = await this.edit()
          break
        case 'view':
          res = true
          break
        default:
          break
      }
      return res
    },
    async add () {
      try {
        const res = await this.$refs.form.validate()
        if (res) {
          const params = {
            industryType: this.beanInfo.industryType,
            destCatalogCd: this.beanInfo.destCatalogCd,
            destChannel: this.beanInfo.destChannel,
          }
          const bool = await this.saveCheck(params)
          if (bool) {
            const res = await this.$post('/admin/merParam/industryMapInfo', params)
            if (res.status === 200) {
              this.$message.success('新增成功！')
              return true
            } else {
              this.$message.error(res.data.message)
              return false
            }
          }
        }
      } catch (e) {
        console.error(e)
      }
    },
    async edit () {
      const res = await this.$refs.form.validate()
      if (res) {
        const params = {
          id: this.beanInfo.id,
          industryType: this.beanInfo.industryType,
          destCatalogCd: this.beanInfo.destCatalogCd,
          destChannel: this.beanInfo.destChannel,
        }
        const bool = await this.saveCheck(params)
        if (bool) {
          const res = await this.$put('/admin/merParam/industryMapInfo/update', params)
          if (res.status === 200) {
            this.$message.success('保存成功！')
            return true
          } else {
            this.$message.error(res.data.message)
            return false
          }
        }
      }
    },
    async saveCheck (beanInfo) {
      try {
        const res = await this.$post('/admin/merParam/industryMapInfo/check', beanInfo)
        const bool = res.status === 200
        !bool && this.$message.error(res.data.message)
        return bool
      } catch (e) {
        console.error(e)
      }
    },
    resetFields () {
      return this.$refs.form.resetFields()
    },
    setBeanInfo (beanInfo) {
      console.log(beanInfo)
      this.beanInfo = beanInfo
      // this.getOne(beanInfo.inspectType,beanInfo.inspectTaskNo)
    },
    async getOne (instId,inspectTaskNo) {
      try {9
        const res = await this.$get('/admin/merParam/inspectTaskInfo/selectByinstIdAndinspectTaskNo', {instId,inspectTaskNo})
        this.beanInfo = res.data
      } catch (e) {
        console.error(e)
      }
    },
  }
}
</script>

<style scoped>
.section {
  background: #fff;
}

.form {
  text-align: center;
}
.input {
  width: 100%;
}

.btn-group {
  margin-bottom: 20px;
}
</style>
